<?php

namespace app\admin\controller;
use think\Db;

class Active
{
    public function __construct()
    {
        Admin_auth();
    }

    public function index()
    {
        $pid = request()->request('pid');
        $cid = request()->request('cid');
        $aid = request()->request('aid');
        $title = request()->request('title');
        if(!$title){
            $title = '邀请送好礼';
        }
        $data = [];
        if ($aid) {
            if (request()->isPost()) {
                $data = request()->post();
                if(empty($data['redpacket'])){
                    echo '请设置优惠券';
                }else{
					$data['redpacket'] = implode(',',$data['redpacket']);
                    Db::table("active")->where(['title' => $data['title'], 'aid' => $aid])->update($data);
                }
            }else{
                $count = Db::table('active')->where(['aid'=>$aid])->count();
                if($count == 0){
                    Db::table('active')->insert(['pid'=>$pid,'cid'=>$cid,'aid'=>$aid,'title'=>'邀请送好礼','description'=>'邀请注册即送大奖！','status'=>1,'redpacket'=>'']);
                    Db::table('active')->insert(['pid'=>$pid,'cid'=>$cid,'aid'=>$aid,'title'=>'新用户注册','description'=>'新用户注册送大奖！','status'=>1,'redpacket'=>'']);
										Db::table('active')->insert(['pid'=>$pid,'cid'=>$cid,'aid'=>$aid,'title'=>'分享订单','description'=>'分享订单送大礼！','status'=>1,'redpacket'=>'']);
                }
            }
            $redpacketlist = Db::table("redpacket_list")->where(['aid' => $aid])->select();
            $data = Db::table("active")->where(['title'=>$title,'aid' => $aid])->find();
            if(!empty($data['redpacket'])){
                $data['redpacket'] = explode(',',$data['redpacket']);
            }
        }
        return view('', ['redpacketlist' => $redpacketlist,'data' => $data, 'aid' => $aid]);
    }
}